package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.File;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class FileDao extends BaseDao {
	// property constants
	public static final String FILE_NAME = "fileName";
	public static final String FILE_PATH = "filePath";

	public int delete(File persistentInstance) {
		try {
			String sql = "delete from file where fileId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getFileId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<File> findAll() {
		try {
			String sql = "select * from file";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<File> findByFileName(Object fileName) {
		return findByProperty(FILE_NAME, fileName);
	}

	public List<File> findByFilePath(Object filePath) {
		return findByProperty(FILE_PATH, filePath);
	}

	public File findById(java.lang.Integer id) {
		try {
			String sql = "select * from file where fileId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<File> instanceList = this
					.getListByResultSet(ps.executeQuery());
			if (instanceList == null) {
				return null;
			}
			File instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<File> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from file where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	@Override
	protected List<File> getListByResultSet(ResultSet rs) {
		// TODO Auto-generated method stub
		List<File> list = new ArrayList<File>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				File file = new File();
				file.setFileId(rs.getInt("fileId"));
				file.setFileName(rs.getString("fileName"));
				file.setFilePath(rs.getString("filePath"));
				list.add(file);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(File transientInstance) {
		try {
			String sql = "insert into file(fileName, filePath) values(?, ?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, transientInstance.getFileName());
			ps.setString(2, transientInstance.getFilePath());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {
		// TODO Auto-generated method stub

	}

	public int update(File instance) {
		try {
			String sql = "update file set fileName=?, filePath=? where fileId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, instance.getFileName());
			ps.setString(2, instance.getFilePath());
			ps.setInt(3, instance.getFileId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}